<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productName">
              <a-input type="text" v-model="model.productName" placeholder="请输入产品名称"></a-input>
            </a-form-model-item>
            <a-form-model-item label="产品品码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productZxxsdycpbs">
              <a-input
                type="text"
                v-model="model.productZxxsdycpbs"
                placeholder="请输入产品品码"
                @blur="queryPinCodeFun"
              ></a-input>
            </a-form-model-item>
            <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productSpec">
              <a-input type="text" v-model="model.productSpec" placeholder="请输入规格型号"></a-input>
            </a-form-model-item>
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productUnit">
              <a-select
                v-model="model.productUnit"
                style="width: 100%"
                placeholder="请选择"
                :options="unitList"
              ></a-select>
            </a-form-model-item>
            <a-form-model-item label="生产厂商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productCpbsbmtxmc">
              <a-input type="text" v-model="model.productCpbsbmtxmc" placeholder="请输入生产厂商"></a-input>
            </a-form-model-item>
            <a-form-model-item label="产品属性" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productAttributes">
              <a-select
                v-model="model.productAttributes"
                style="width: 100%"
                placeholder="请选择"
                :options="propertyList"
              ></a-select>
            </a-form-model-item>
            <!-- <a-form-model-item label="库存上限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input type="number" v-model="model.productCpbsbmtxmc" placeholder="请输入生产厂商"></a-input>
            </a-form-model-item>
            <a-form-model-item label="库存下限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input type="number" v-model="model.sums" placeholder="请输入打印数量"></a-input>
            </a-form-model-item> -->
            <a-form-model-item label="产品单重(克)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="singleWeight">
              <a-input type="number" v-model="model.singleWeight" placeholder="请输入产品单重"></a-input>
            </a-form-model-item>

            <a-form-model-item label="包装数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productZxxsydydsl">
              <a-input type="number" v-model="model.productZxxsydydsl" placeholder="请输入包装数"></a-input>
            </a-form-model-item>
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isEnabled">
              <a-radio-group v-model="model.isEnabled" name="radioGroup" :default-value="1">
                <a-radio :value="1"> 是 </a-radio>
                <a-radio :value="0"> 否 </a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import { queryProductInfoUnitApi, queryProductInfoProductApi, queryProductPinCodeApi } from '@/api/good.js'
export default {
  name: 'BmsSupplierForm',
  components: {
    JDictSelectTag,
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        // productZxxsdycpbs: null, // 产品品码
        // productName: null,
        // productCpbsbmtxmc: null,
        // productAttributes: null,
        // productSpec: null,
        // productUnit: null,
        // productYlqxzcrbarmc: null,
        // singleWeight: null,
        // inventoryUpperLimit: null, // 库存上限
        // inventoryLowerLimit: null, // 库存下限
        // isEnabled: 1, // 是否启用 0未启用，1启用
        isEnabled: 1,
        productType: 0,
      },

      unitList: [],
      propertyList: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        productName: [{ required: true, message: '请输入产品名称!' }],
        productZxxsdycpbs: [{ required: true, message: '请输入产品品码!' }],
        productSpec: [{ required: true, message: '请输入规格型号!' }],
        productUnit: [{ required: true, message: '请选择单位!' }],
        productCpbsbmtxmc: [{ required: true, message: '请输入生产厂商!' }],
        productAttributes: [{ required: true, message: '请选择产品属性!' }],
        // singleWeight: [{ required: true, message: '请输入产品单重!' }],
        productZxxsydydsl: [{ required: true, message: '请输入产品包装数!' }],
        productZxxsdycpbs: [{ required: true, message: '请输入产品品码!' }],
      },
      url: {
        add: '/bms/bmsProductInfo/simpleAdd',
        edit: '/bms/bmsProductInfo/edit',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  mounted() {
    this.getProductUnitList()
    this.getProductPropertyList()
  },
  methods: {
    async queryPinCodeFun() {
      if (this.model.productZxxsdycpbs == '') return
      try {
        const r = await queryProductPinCodeApi({
          zxxsdycpbs: this.model.productZxxsdycpbs,
        })
        if (r.code == 200) {
          this.$message.success('该品码可以使用')
        }
      } catch (error) {
        this.model.productZxxsdycpbs = ''
        this.$message.error(error.data.message)
      }
    },
    async getProductUnitList() {
      try {
        const r = await queryProductInfoUnitApi({ pageNo: 1, pageSize: 10 })
        this.unitList = r.result.records.map((e) => {
          return { ...e, label: e.unitName, value: e.unitName }
        })
      } catch (err) {
        console.log(err)
        this.$message.error(err)
      }
    },
    async getProductPropertyList() {
      try {
        const r = await queryProductInfoProductApi({ pageNo: 1, pageSize: 10 })
        this.propertyList = r.result.records.map((e) => {
          return { ...e, label: e.unitName, value: e.unitName }
        })
      } catch (err) {
        console.log(err)
        this.$message.error(err)
      }
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      record.isEnabled = record.isEnabled * 1
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              console.log(res)
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>

